package com.xhu.water_supply.web.controller;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xhu.water_supply.dto.*;
import com.xhu.water_supply.entity.Order;
import com.xhu.water_supply.mapper.OrderMapper;
import com.xhu.water_supply.query.OrderQuery;
import com.xhu.water_supply.service.OrderService;
import com.xhu.water_supply.service.WorkerService;
import com.xhu.water_supply.web.vo.OrderVO;
import com.xhu.water_supply.web.vo.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@CrossOrigin
@Api(tags = "订单信息")
@RequestMapping("/api/Order")
public class OrderController {

    @Autowired
    private OrderService orderService;



    @GetMapping("/selectAll")
    @CrossOrigin
    public PageVO<Order> selectAll(AdminInfoAllDTO adminInfoAllDTO){
        Page<Order> page= PageHelper.startPage(adminInfoAllDTO.getPageNO(), adminInfoAllDTO.getPageSize());
        List<Order> orderList=orderService.selectAll();
        PageVO<Order> pageVO = new PageVO<>(orderList, adminInfoAllDTO.getPageNO(), adminInfoAllDTO.getPageSize(),(int)page.getTotal());
        return pageVO;
//        List<OrderAddDTO> dtoList =new ArrayList<>();
//        orderList.forEach(temp->{
//            OrderAddDTO addDTO = OrderAddDTO.builder()
//                    .no(temp.getNo())
//                    .workId(orderService.getWorkerName(temp.getWorkId()))
//                    .customerId(orderService.getCustomerName(temp.getCustomerId()))
//                    .date(temp.getDate())
//                    .num(temp.getNum())
//                    .build();
//            dtoList.add(addDTO);
//        });

    }

    @CrossOrigin
    @PostMapping("/insert")
    public Result insert(OrderAddDTO orderAddDTO) throws ParseException {
//        Order order= Order.builder()
//                .workId(orderService.SelectIdByName(orderAddDTO.getWorkId()))
//                .customerId(orderService.SearchIdByName(orderAddDTO.getCustomerId()))
//                .date(orderAddDTO.getDate())
//                .num(orderAddDTO.getNum())
//                .no(orderAddDTO.getNo())
//                .build();
        Order order=new Order();
        System.out.println(orderAddDTO);
        BeanUtils.copyProperties(orderAddDTO,order);
        int result=orderService.insert(order);
        if(result > 0)
            return ResultUtils.returnSuccess(ResultEnum.SUCCESS.getMsg());
        else
            return ResultUtils.returnFail(ResultEnum.FAIL.getMsg());
    }

    @CrossOrigin
    @GetMapping("/del/{no}")
    public Result del(@PathVariable int no){
        int result= orderService.deleteByPrimaryKey(no);
        if(result>0)
            return ResultUtils.returnSuccess(ResultEnum.SUCCESS.getMsg());
        else
            return ResultUtils.returnFail(ResultEnum.FAIL.getMsg());
    }

    @CrossOrigin
    @PostMapping("/modify")
    public Result modify(OrderAddDTO orderAddDTO) throws ParseException {
//        Order order= Order.builder()
//                .workId(orderService.SelectIdByName(orderAddDTO.getWorkId()))
//                .customerId(orderService.SearchIdByName(orderAddDTO.getCustomerId()))
//                .date(orderAddDTO.getDate())
//                .num(orderAddDTO.getNum())
//                .no(orderAddDTO.getNo())
//                .build();
        Order order=new Order();
        System.out.println(orderAddDTO);
        BeanUtils.copyProperties(orderAddDTO,order);
        int result=orderService.updateByPrimaryKey(order);
        if(result > 0)
            return ResultUtils.returnSuccess(ResultEnum.SUCCESS.getMsg());
        else
            return ResultUtils.returnFail(ResultEnum.FAIL.getMsg());
        }

    @CrossOrigin
    @GetMapping("/selectByNo/{no}")
    public Order selectOne(@PathVariable int no){
        return orderService.selectByPrimaryKey(no);
    }

    @CrossOrigin
    @GetMapping("/selectWorkerId")
    public List<Integer> selectWorkerId(){
        return orderService.selectAllWokerId();
    }
    @CrossOrigin
    @GetMapping("/selectCusId")
    public List<Integer> selectCustomerId(){
        return orderService.selectAllCusId();
    }

    @ApiOperation(value = "分页模糊查询订单信息接口")
    @CrossOrigin
    @GetMapping("/queryOrder")
    public Result<PageVO<OrderVO>> queryOrder(OrderQuery orderQuery) {
        Page<Order> page = PageHelper.startPage(orderQuery.getPageNo(), orderQuery.getPageSize());
        List<Order> orderList = orderService.selectOrderQuery(orderQuery);
        List<OrderVO> orderVos = new ArrayList<>();
        {
            orderList.forEach(order -> {
                OrderVO vo = new OrderVO();
                //同名属性的复制
                BeanUtils.copyProperties(order, vo);
                orderVos.add(vo);
            });
            PageVO<OrderVO> pageVO = new PageVO<OrderVO>(orderVos, orderQuery.getPageNo(), orderQuery.getPageSize(), (int) page.getTotal());
            return ResultUtils.returnSuccess(ResultEnum.SUCCESS.getMsg(), pageVO);
        }
    }
    @CrossOrigin
    @GetMapping("/multiDelete/{orderIds}")
    public Result multiDelete(@PathVariable String orderIds){
        String Ids[]=orderIds.split(",");
        int orderId[]= Arrays.stream(Ids).mapToInt(Integer::parseInt).toArray();
        if (orderService.multiDelete(orderId) == orderId.length)
        {
            return ResultUtils.returnSuccess(ResultEnum.SUCCESS.getMsg(),orderIds);
        }
        return ResultUtils.returnSuccess(ResultEnum.FAIL.getMsg(),orderIds);

    }
}
